{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "323d39e2",
   "metadata": {},
   "source": [
    "# Javascript to Python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7fa042d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'function $(a) { [python code] }'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import js2py\n",
    "f = js2py.eval_js( \"function $(a) {return a + arguments[1]}\" )\n",
    "f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "96b5a72f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f(1, 2, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d94f115c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "from js2py.pyjs import *\n",
      "# setting scope\n",
      "var = Scope( JS_BUILTINS )\n",
      "set_global_object(var)\n",
      "\n",
      "# Code follows:\n",
      "var.registers(['$'])\n",
      "@Js\n",
      "def PyJsHoistedNonPyName(a, this, arguments, var=var):\n",
      "    var = Scope({'a':a, 'this':this, 'arguments':arguments}, var)\n",
      "    var.registers(['a'])\n",
      "    return (var.get('a')+var.get('arguments').get('1'))\n",
      "PyJsHoistedNonPyName.func_name = '$'\n",
      "var.put('$', PyJsHoistedNonPyName)\n",
      "pass\n",
      "pass\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(js2py.translate_js( \"function $(a) {return a + arguments[1]}\" ))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e1485387",
   "metadata": {},
   "outputs": [],
   "source": [
    "from js2py.pyjs import *\n",
    "# setting scope\n",
    "var = Scope( JS_BUILTINS )\n",
    "set_global_object(var)\n",
    "\n",
    "# Code follows:\n",
    "var.registers(['$'])\n",
    "@Js\n",
    "def PyJsHoistedNonPyName(a, this, arguments, var=var):\n",
    "    var = Scope({'a':a, 'this':this, 'arguments':arguments}, var)\n",
    "    var.registers(['a'])\n",
    "    return (var.get('a')+var.get('arguments').get('1'))\n",
    "PyJsHoistedNonPyName.func_name = '$'\n",
    "var.put('$', PyJsHoistedNonPyName)\n",
    "pass\n",
    "pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "292cf983",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'3'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "PyJsHoistedNonPyName(1, 2, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b5251f50",
   "metadata": {},
   "outputs": [],
   "source": [
    "js_str = \"var ExchangProcessor={\n",
    "\n",
    "\tcreateNew: function(exA,exB){\n",
    "\t\tvar contract_type\t\t='this_week';\t\t//合约类型\n",
    "\t\tvar margin_level\t\t=10;\t\t\t\t//杠杆倍数\n",
    "\t\tvar want_profit\t\t\t=0.01;\t\t\t\t//想要的额外收入(+汇率损失)\n",
    "\t\tvar ignore_range\t\t=0.001;\t\t\t\t//例如USDT对USD, 对冲期的安全波动范围，策略会忽略这个范围的波动\n",
    "\t\tvar max_wait_order\t\t=10000;\t\t\t\t//订单等待\n",
    "\t\tvar wait_ms\t\t\t\t=3000;\t\t\t\t//默认等待毫秒\n",
    "\t\tvar traders_recorder\t=false;\t\t\t\t//记录所有交易\n",
    "\n",
    "\t\tvar handfee=\t {OKEX:0.001,\tFutures_OKCoin:0.001};\t\t\t\t//手续费\n",
    "\t\tvar trade_amount={ETH_USD:0.5,\tBCH_USD:0.5,\tBTC_USD:0.05};\t\t//每次交易数量 例如0.5个bch\n",
    "\t\tvar contract_min={ETH_USD:10,\tBCH_USD:10,\t\tBTC_USD:100};\t\t//一合约价值多少美金 USD,用这个计算买入张数\n",
    "\n",
    "\t\tvar price_n\t\t={ETH_USDT:4,\t\tETH_USD:3,\t\tBCH_USDT:4,\t\tBCH_USD:3,\t\tBTC_USDT:4,\t\tBTC_USD:2}; \t//价格精度\n",
    "\t\tvar num_n\t\t={ETH_USDT:3,\t\tETH_USD:0,\t\tBCH_USDT:3,\t\tBCH_USD:0,\t\tBTC_USDT:3,\t\tBTC_USD:0}; \t//数量精度\n",
    "\t\tvar minestbuy\t={ETH_USDT:0.001,\tETH_USD:1,\t\tBCH_USDT:0.001,\tBCH_USD:1,\t\tBTC_USDT:0.001,\tBTC_USD:1}; \t//最小买入量\n",
    "\t\tvar price_step\t={ETH_USDT:0.03,\tETH_USD:0.03,\tBCH_USDT:0.05,\tBCH_USD:0.05,\tBTC_USDT:0.65,\tBTC_USD:0.65}; \t//定价单调整价格的单位(每次调整万1, 大致等于25分钟调整%1)\n",
    "\t\tvar pre_time=null; \t\t//记录轮询间隔时间\n",
    "\t\tvar limit_orders=[];\t//限价单\n",
    "\t\tvar trades=[];\t\t\t//所有交易\n",
    "\t\tvar pending_pos=[];\t\t//未完成对冲仓位\n",
    "\t\tvar hedging_op=0;\t\t//对冲机会\n",
    "\t\tvar hedging_real=0;\t\t//真实对冲次数\n",
    "\t\tvar hedging_complete=0;\t//对冲完成次数\n",
    "\n",
    "\t\tvar processor={}\n",
    "\n",
    "\t\tprocessor.retryBuy=function(ex,price,num,mode)\n",
    "\t\t{\n",
    "\t\t\tvar currency=ex.GetCurrency()\n",
    "\t\t\tvar r=ex.Buy(_N(price,price_n[currency]), _N(num,num_n[currency]))\n",
    "\t\t\tvar tempnum=num\n",
    "\t\t\twhile (!r){\n",
    "\t\t\t\tLog('Buy失败，正在重试。')\n",
    "\t\t\t\tSleep(wait_ms)\n",
    "\t\t\t\tif (mode==='spot'){\n",
    "\t\t\t\t\tvar account=_C(ex.GetAccount)\n",
    "\t\t\t\t\tvar ticker=_C(ex.GetTicker)\n",
    "\t\t\t\t\tvar last=ticker.Last\n",
    "\t\t\t\t\tvar fixedAmount=Math.min(account.Balance*0.95/last,num)\n",
    "\t\t\t\t\tr=ex.Buy(_N(price,price_n[currency]), _N(fixedAmount,num_n[currency]))\n",
    "\t\t\t\t}else if(mode==='futures'){\n",
    "\t\t\t\t\t//tempnum=tempnum-1\n",
    "\t\t\t\t\tif (tempnum===0){\n",
    "\t\t\t\t\t\tbreak\n",
    "\t\t\t\t\t};\n",
    "\t\t\t\t\tr=ex.Buy(_N(price,price_n[currency]), _N(tempnum,num_n[currency]));\n",
    "\t\t\t\t};\n",
    "\t\t\t};\n",
    "\t\t\treturn r;\n",
    "\t\t};\n",
    "\n",
    "\t\tprocessor.retrySell=function(ex,price,num,mode){\n",
    "\t\t\tvar currency=ex.GetCurrency()\n",
    "\t\t\tvar r=ex.Sell(_N(price,price_n[currency]), _N(num,num_n[currency]))\n",
    "\t\t\tvar tempnum=num\n",
    "\t\t\twhile (!r){\n",
    "\t\t\t\tLog('Sell失败，正在重试。')\n",
    "\t\t\t\tSleep(wait_ms)\n",
    "\t\t\t\tif (mode==='spot'){\n",
    "\t\t\t\t\tvar account=_C(ex.GetAccount)\n",
    "\t\t\t\t\tvar fixedAmount=Math.min(account.Stocks,num)\n",
    "\t\t\t\t\tr=ex.Sell(_N(price,price_n[currency]), _N(fixedAmount,num_n[currency]))\n",
    "\t\t\t\t}else if(mode==='futures'){\n",
    "\t\t\t\t\t//tempnum=tempnum-1\n",
    "\t\t\t\t\tvar position=_C(ex.GetPosition)\n",
    "\t\t\t\t\tif (tempnum===0or position.length===0){\n",
    "\t\t\t\t\t\tbreak\n",
    "\t\t\t\t\t};\n",
    "\t\t\t\t\tr=ex.Sell(_N(price,price_n[currency]), _N(tempnum,num_n[currency]));\n",
    "\n",
    "\t\t\t\t};\n",
    "\t\t\t};\n",
    "\t\t\treturn r;\n",
    "\t\t};\n",
    "\n",
    "\t\tprocessor.getUSDTratio=function(){\n",
    "\t\t\tvar r = _C(HttpQuery,'https://api.coinmarketcap.com/v2/ticker/825/')\n",
    "\t\t\tvar o = JSON.parse(r)\n",
    "\t\t\treturn o.data.quotes.USD.price\n",
    "\t\t};\n",
    "\n",
    "\t\tprocessor.get_ChinaTimeString=function(){\n",
    "\t\t\tvar date = new Date()\n",
    "\t\t\tvar now_utc =  Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(),date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds())\n",
    "\t\t\tvar cdate=new Date(now_utc)\n",
    "\t\t\tcdate.setHours(cdate.getHours()+8)\n",
    "\t\t\tvar localstring=cdate.getFullYear()+'/'+(cdate.getMonth()+1)+'/'+cdate.getDate()+' '+cdate.getHours()+':'+cdate.getMinutes()+':'+cdate.getSeconds()\n",
    "\t\t\treturn localstring\n",
    "\t\t};\n",
    "\n",
    "\t\tprocessor.process_limiteorders=function(){\n",
    "\t\t\tvar cur_time=new Date()\n",
    "\t\t\tvar limit_orders_new=[]\n",
    "\t\t\tfor (var i=0; i<limit_orders.length; ++i){\n",
    "\t\t\t\tvar create_time=limit_orders[i].create_time\n",
    "\t\t\t\tvar passedtime=cur_time-create_time\n",
    "\t\t\t\tif (passedtime>max_wait_order){\n",
    "\t\t\t\t\tvar exchange_c=limit_orders[i].exchange\n",
    "\t\t\t\t\tvar order_ID=limit_orders[i].ID\n",
    "\t\t\t\t\tvar ordermode=limit_orders[i].mode\n",
    "\t\t\t\t\tvar exname=exchange_c.GetName()\n",
    "\t\t\t\t\tvar account=_C(exchange_c.GetAccount)\n",
    "\t\t\t\t\tvar ticker=_C(exchange_c.GetTicker)\n",
    "\t\t\t\t\tvar last=ticker.Last\n",
    "\t\t\t\t\tvar orderdata=_C(exchange_c.GetOrder,order_ID)\n",
    "\t\t\t\t\tvar type=orderdata.Type\n",
    "\t\t\t\t\tvar currency=exchange_c.GetCurrency()\n",
    "\n",
    "\t\t\t\t\tif (orderdata.Status!=ORDER_STATE_CLOSED){\n",
    "\t\t\t\t\t\tvar notcompleted=orderdata.Amount-orderdata.DealAmount\n",
    "\t\t\t\t\t\texchange_c.CancelOrder(order_ID)\n",
    "\t\t\t\t\t\tif (type===ORDER_TYPE_BUY){\n",
    "\t\t\t\t\t\t\tvar allowbuy=notcompleted\n",
    "\t\t\t\t\t\t\tif (allowbuy>=minestbuy[currency]){\n",
    "\t\t\t\t\t\t\t\tvar limited_order_ID=processor.retryBuy(exchange_c,orderdata.Price+price_step[currency],allowbuy,ordermode)\n",
    "\t\t\t\t\t\t\t\tLog('加价买入。'+orderdata.Price+'|'+(orderdata.Price+price_step[currency]))\n",
    "\t\t\t\t\t\t\t\tvar limited_order_data={\n",
    "\t\t\t\t\t\t\t\t\texchange:exchange_c,\n",
    "\t\t\t\t\t\t\t\t\tID:limited_order_ID,\n",
    "\t\t\t\t\t\t\t\t\tcreate_time:new Date(),\n",
    "\t\t\t\t\t\t\t\t\tutcdate:processor.get_ChinaTimeString(),\n",
    "\t\t\t\t\t\t\t\t\tmode:ordermode\n",
    "\t\t\t\t\t\t\t\t}\n",
    "\t\t\t\t\t\t\t\tlimit_orders_new.push(limited_order_data)\n",
    "\t\t\t\t\t\t\t};\n",
    "\t\t\t\t\t\t}else if (type===ORDER_TYPE_SELL){\n",
    "\t\t\t\t\t\t\tvar allowsell=notcompleted\n",
    "\t\t\t\t\t\t\tif (allowsell>=minestbuy[currency]){\n",
    "\t\t\t\t\t\t\t\tvar limited_order_ID=processor.retrySell(exchange_c,orderdata.Price-price_step[currency],allowsell,ordermode)\n",
    "\t\t\t\t\t\t\t\tLog('降价卖出。'+orderdata.Price+'|'+(orderdata.Price-price_step[currency]))\n",
    "\t\t\t\t\t\t\t\tvar limited_order_data={\n",
    "\t\t\t\t\t\t\t\t\texchange:exchange_c,\n",
    "\t\t\t\t\t\t\t\t\tID:limited_order_ID,\n",
    "\t\t\t\t\t\t\t\t\tcreate_time:new Date(),\n",
    "\t\t\t\t\t\t\t\t\tutcdate:processor.get_ChinaTimeString(),\n",
    "\t\t\t\t\t\t\t\t\tmode:ordermode\n",
    "\t\t\t\t\t\t\t\t}\n",
    "\t\t\t\t\t\t\t\tlimit_orders_new.push(limited_order_data)\n",
    "\t\t\t\t\t\t\t};\n",
    "\t\t\t\t\t\t};\n",
    "\n",
    "\t\t\t\t\t\t//保存交易信息;\n",
    "\t\t\t\t\t\tif (type===ORDER_TYPE_BUY){\n",
    "\t\t\t\t\t\t\tvar details={\n",
    "\t\t\t\t\t\t\t\ttype:'限价买',\n",
    "\t\t\t\t\t\t\t\ttime:limit_orders[i].utcdate,\n",
    "\t\t\t\t\t\t\t\tRealAmount:orderdata.DealAmount,\n",
    "\t\t\t\t\t\t\t\tWantAmount:orderdata.Amount,\n",
    "\t\t\t\t\t\t\t\tRealPrice:orderdata.Price,\n",
    "\t\t\t\t\t\t\t\tWantPrice:orderdata.Price,\n",
    "\t\t\t\t\t\t\t\tMemo:'部分完成',\n",
    "\t\t\t\t\t\t\t\texname:exname\n",
    "\t\t\t\t\t\t\t\t}\n",
    "\t\t\t\t\t\t\tif (traders_recorder){\n",
    "\t\t\t\t\t\t\t\ttrades.push(details)\n",
    "\t\t\t\t\t\t\t};\n",
    "\t\t\t\t\t\t}else if (type===ORDER_TYPE_SELL){\n",
    "\t\t\t\t\t\t\tvar details={\n",
    "\t\t\t\t\t\t\t\ttype:'限价卖',\n",
    "\t\t\t\t\t\t\t\ttime:limit_orders[i].utcdate,\n",
    "\t\t\t\t\t\t\t\tRealAmount:orderdata.DealAmount,\n",
    "\t\t\t\t\t\t\t\tWantAmount:orderdata.Amount,\n",
    "\t\t\t\t\t\t\t\tRealPrice:orderdata.Price,\n",
    "\t\t\t\t\t\t\t\tWantPrice:orderdata.Price,\n",
    "\t\t\t\t\t\t\t\tMemo:'部分完成',\n",
    "\t\t\t\t\t\t\t\texname:exname\n",
    "\t\t\t\t\t\t\t\t}\n",
    "\t\t\t\t\t\t\tif (traders_recorder){\n",
    "\t\t\t\t\t\t\t\ttrades.push(details)\n",
    "\t\t\t\t\t\t\t};\n",
    "\t\t\t\t\t\t};\n",
    "\t\t\t\t\t}else{\n",
    "\t\t\t\t\t\t//保存交易信息\n",
    "\t\t\t\t\t\tif (type===ORDER_TYPE_BUY){\n",
    "\t\t\t\t\t\t\tvar details={\n",
    "\t\t\t\t\t\t\t\ttype:'限价买',\n",
    "\t\t\t\t\t\t\t\ttime:limit_orders[i].utcdate,\n",
    "\t\t\t\t\t\t\t\tRealAmount:orderdata.DealAmount,\n",
    "\t\t\t\t\t\t\t\tWantAmount:orderdata.Amount,\n",
    "\t\t\t\t\t\t\t\tRealPrice:orderdata.Price,\n",
    "\t\t\t\t\t\t\t\tWantPrice:orderdata.Price,\n",
    "\t\t\t\t\t\t\t\tMemo:'已完成',\n",
    "\t\t\t\t\t\t\t\texname:exname\n",
    "\t\t\t\t\t\t\t\t}\n",
    "\t\t\t\t\t\t\tif (traders_recorder){\n",
    "\t\t\t\t\t\t\t\ttrades.push(details)\n",
    "\t\t\t\t\t\t\t};\n",
    "\t\t\t\t\t\t}else if (type===ORDER_TYPE_SELL){\n",
    "\t\t\t\t\t\t\tvar details={\n",
    "\t\t\t\t\t\t\t\ttype:'限价卖',\n",
    "\t\t\t\t\t\t\t\ttime:limit_orders[i].utcdate,\n",
    "\t\t\t\t\t\t\t\tRealAmount:orderdata.DealAmount,\n",
    "\t\t\t\t\t\t\t\tWantAmount:orderdata.Amount,\n",
    "\t\t\t\t\t\t\t\tRealPrice:orderdata.Price,\n",
    "\t\t\t\t\t\t\t\tWantPrice:orderdata.Price,\n",
    "\t\t\t\t\t\t\t\tMemo:'已完成',\n",
    "\t\t\t\t\t\t\t\texname:exname\n",
    "\t\t\t\t\t\t\t\t}\n",
    "\t\t\t\t\t\t\tif (traders_recorder){\n",
    "\t\t\t\t\t\t\t\ttrades.push(details)\n",
    "\t\t\t\t\t\t\t};\n",
    "\t\t\t\t\t\t};\n",
    "\t\t\t\t\t};\n",
    "\t\t\t\t}else{\n",
    "\t\t\t\t\tlimit_orders_new.push(limit_orders[i])\n",
    "\t\t\t\t};\n",
    "\t\t\t};\n",
    "\t\t\tlimit_orders=limit_orders_new;\n",
    "\t\t};\n",
    "\n",
    "\t\tprocessor.force_limited_order=function(type,ex,price,num,mode){\n",
    "\t\t\tif (type==='buy'){\n",
    "\t\t\t\tvar buyID=processor.retryBuy(ex,price,num,mode)\n",
    "\t\t\t\tvar order1={\n",
    "\t\t\t\t\texchange:ex,\n",
    "\t\t\t\t\tID:buyID,\n",
    "\t\t\t\t\tcreate_time:new Date(),\n",
    "\t\t\t\t\tutcdate:processor.get_ChinaTimeString(),\n",
    "\t\t\t\t\tmode:mode\n",
    "\t\t\t\t}\n",
    "\t\t\t\tlimit_orders.push(order1)\n",
    "\t\t\t};\n",
    "\t\t\tif (type==='sell'){\n",
    "\t\t\t\tvar sellID=processor.retrySell(ex,price,num,mode)\n",
    "\t\t\t\tvar order1={\n",
    "\t\t\t\t\texchange:ex,\n",
    "\t\t\t\t\tID:sellID,\n",
    "\t\t\t\t\tcreate_time:new Date(),\n",
    "\t\t\t\t\tutcdate:processor.get_ChinaTimeString(),\n",
    "\t\t\t\t\tmode:mode\n",
    "\t\t\t\t}\n",
    "\t\t\t\tlimit_orders.push(order1)\n",
    "\t\t\t};\n",
    "\n",
    "\t\t\twhile(limit_orders.length!==0){\n",
    "\t\t\t\t//process limited orders\n",
    "\t\t\t\tprocessor.process_limiteorders()\n",
    "\t\t\t\tSleep(wait_ms)\n",
    "\t\t\t};\n",
    "\t\t};\n",
    "\n",
    "\t\tprocessor.init_obj=function(){\n",
    "\t\t\t_CDelay(wait_ms)\n",
    "\t\t\tpre_time = new Date()\n",
    "\t\t};\n",
    "\n",
    "\t\tprocessor.work=function(){\n",
    "\t\t\tvar cur_time = new Date()\n",
    "\t\t\tvar passedtime=cur_time-pre_time\n",
    "\t\t\tpre_time=cur_time\n",
    "\n",
    "\t\t\tvar USDT_r=processor.getUSDTratio()\n",
    "\t\t\tvar A_account=_C(exA.GetAccount)\n",
    "\t\t\tvar A_exname=exA.GetName()\n",
    "\t\t\tvar A_currency=exA.GetCurrency()\n",
    "\t\t\tvar A_quotecurrency=exA.GetQuoteCurrency()\n",
    "\t\t\tvar A_ticker=_C(exA.GetTicker)\n",
    "\t\t\tvar A_last=A_ticker.Last\n",
    "\t\t\tvar A_last_fixed=A_last*USDT_r;\t//Fixed price: ETH/USD\n",
    "\t\t\tvar A_depth=_C(exA.GetDepth)\n",
    "\t\t\tvar A_orders=_C(exA.GetOrders)\n",
    "\t\t\tvar A_buy1=A_depth.Bids[0].Price\n",
    "\t\t\tvar A_sell1=A_depth.Asks[0].Price\n",
    "\t\t\texB.SetContractType(contract_type)\n",
    "\t\t\texB.SetMarginLevel(margin_level)\n",
    "\t\t\tvar B_account=_C(exB.GetAccount)\n",
    "\t\t\tvar B_exname=exB.GetName()\n",
    "\t\t\tvar B_currency=exB.GetCurrency()\n",
    "\t\t\tvar B_quotecurrency=exB.GetQuoteCurrency()\n",
    "\t\t\tvar B_ticker=_C(exB.GetTicker)\n",
    "\t\t\tvar B_last=B_ticker.Last\n",
    "\t\t\tvar B_depth=_C(exB.GetDepth)\n",
    "\t\t\tvar B_orders=_C(exB.GetOrders)\n",
    "\t\t\tvar B_buy1=B_depth.Bids[0].Price\n",
    "\t\t\tvar B_sell1=B_depth.Asks[0].Price\n",
    "\t\t\tvar cdiff=Math.abs(B_last-A_last_fixed)/A_last_fixed\n",
    "\t\t\tvar cdiff2=(B_last-A_last_fixed)/A_last_fixed\n",
    "\t\t\tvar target_bofu=want_profit+handfee[A_exname]*2+handfee[B_exname]*2+ignore_range\n",
    "\n",
    "\t\t\tif (limit_orders.length===0 && A_last_fixed>B_last && cdiff>target_bofu){\n",
    "\t\t\t\thedging_op++\n",
    "\t\t\t\tvar heyuefenshu_B=_N(trade_amount[B_currency]*B_last/contract_min[B_currency],0)\n",
    "\t\t\t\tvar exact_amount=heyuefenshu_B*contract_min[B_currency]/B_last\n",
    "\n",
    "\t\t\t\tif (A_account.Stocks*0.9>exact_amount && B_account.Stocks*0.9>exact_amount){\n",
    "\t\t\t\t\thedging_real++\n",
    "\t\t\t\t\tprocessor.force_limited_order('sell',exA,A_buy1,exact_amount,'spot')\n",
    "\t\t\t\t\texB.SetDirection('buy')\n",
    "\t\t\t\t\tprocessor.force_limited_order('buy',exB,B_sell1,heyuefenshu_B,'futures')\n",
    "\n",
    "\t\t\t\t\tvar posA={\n",
    "\t\t\t\t\t\ttype:'spot-sell',\n",
    "\t\t\t\t\t\texchange:exA,\n",
    "\t\t\t\t\t\tprice:A_buy1,\n",
    "\t\t\t\t\t\tamount:exact_amount,\n",
    "\t\t\t\t\t\tstarttime:processor.get_ChinaTimeString()\n",
    "\t\t\t\t\t}\n",
    "\t\t\t\t\tpending_pos.push(posA)\n",
    "\t\t\t\t\tvar posB={\n",
    "\t\t\t\t\t\ttype:'futures-buyup',\n",
    "\t\t\t\t\t\texchange:exB,\n",
    "\t\t\t\t\t\tprice:B_sell1,\n",
    "\t\t\t\t\t\tamount:heyuefenshu_B,\n",
    "\t\t\t\t\t\tstarttime:processor.get_ChinaTimeString()\n",
    "\t\t\t\t\t}\n",
    "\t\t\t\t\tpending_pos.push(posB)\n",
    "\n",
    "\t\t\t\t};\n",
    "\n",
    "\t\t\t};\n",
    "\t\t\tif (limit_orders.length===0 && A_last_fixed<B_last && cdiff>target_bofu){\n",
    "\t\t\t\thedging_op++\n",
    "\n",
    "\t\t\t\tvar heyuefenshu_B=_N(trade_amount[B_currency]*B_last/contract_min[B_currency],0)\n",
    "\t\t\t\tvar exact_amount=heyuefenshu_B*contract_min[B_currency]/B_last\n",
    "\n",
    "\t\t\t\tif (A_account.Balance*0.9>exact_amount*A_last && B_account.Stocks*0.9>exact_amount){\n",
    "\t\t\t\t\thedging_real++\n",
    "\t\t\t\t\tprocessor.force_limited_order('buy',exA,A_sell1,exact_amount,'spot')\n",
    "\t\t\t\t\texB.SetDirection('sell')\n",
    "\t\t\t\t\tprocessor.force_limited_order('buy',exB,B_sell1,heyuefenshu_B,'futures')\n",
    "\n",
    "\t\t\t\t\tvar posA={\n",
    "\t\t\t\t\t\ttype:'spot-buy',\n",
    "\t\t\t\t\t\texchange:exA,\n",
    "\t\t\t\t\t\tprice:A_sell1,\n",
    "\t\t\t\t\t\tamount:exact_amount,\n",
    "\t\t\t\t\t\tstarttime:processor.get_ChinaTimeString()\n",
    "\t\t\t\t\t}\n",
    "\t\t\t\t\tpending_pos.push(posA)\n",
    "\t\t\t\t\tvar posB={\n",
    "\t\t\t\t\t\ttype:'futures-buydown',\n",
    "\t\t\t\t\t\texchange:exB,\n",
    "\t\t\t\t\t\tprice:B_sell1,\n",
    "\t\t\t\t\t\tamount:heyuefenshu_B,\n",
    "\t\t\t\t\t\tstarttime:processor.get_ChinaTimeString()\n",
    "\t\t\t\t\t}\n",
    "\t\t\t\t\tpending_pos.push(posB)\n",
    "\n",
    "\t\t\t\t};\n",
    "\t\t\t};\n",
    "\n",
    "\t\t\tif (limit_orders.length===0 && cdiff<=ignore_range && A_orders.length===0 && B_orders.length===0 && pending_pos.length>0){\n",
    "\n",
    "\t\t\t\thedging_complete++\n",
    "\n",
    "\t\t\t\tfor (var thidx=0; thidx<pending_pos.length; ++thidx){\n",
    "\t\t\t\t\tvar posinfo=pending_pos[thidx]\n",
    "\t\t\t\t\tvar ticker=_C(posinfo.exchange.GetTicker)\n",
    "\t\t\t\t\tvar last=ticker.Last\n",
    "\t\t\t\t\tvar depth=_C(posinfo.exchange.GetDepth)\n",
    "\t\t\t\t\tvar buy1=depth.Bids[0].Price\n",
    "\t\t\t\t\tvar sell1=depth.Asks[0].Price\n",
    "\t\t\t\t\tvar account=_C(posinfo.exchange.GetAccount)\n",
    "\t\t\t\t\tif (posinfo.type==='futures-buydown'){\n",
    "\t\t\t\t\t\tposinfo.exchange.SetDirection('closesell')\n",
    "\t\t\t\t\t\tprocessor.force_limited_order('sell',posinfo.exchange,buy1,posinfo.amount,'futures')\n",
    "\t\t\t\t\t};\n",
    "\t\t\t\t\tif (posinfo.type==='futures-buyup'){\n",
    "\t\t\t\t\t\tposinfo.exchange.SetDirection('closebuy')\n",
    "\t\t\t\t\t\tprocessor.force_limited_order('sell',posinfo.exchange,buy1,posinfo.amount,'futures')\n",
    "\t\t\t\t\t};\n",
    "\n",
    "\t\t\t\t\tif (posinfo.type==='spot-buy'){\n",
    "\t\t\t\t\t\tprocessor.force_limited_order('sell',posinfo.exchange,buy1,Math.min(account.Stocks,posinfo.amount),'spot')\n",
    "\t\t\t\t\t};\n",
    "\t\t\t\t\tif (posinfo.type==='spot-sell'){\n",
    "\t\t\t\t\t\tprocessor.force_limited_order('buy',posinfo.exchange,sell1,Math.min(account.Balance/last,posinfo.amount),'spot')\n",
    "\t\t\t\t\t};\n",
    "\t\t\t\t};\n",
    "\n",
    "\t\t\t\tpending_pos=[];\n",
    "\t\t\t};\n",
    "\n",
    "\t\t\tvar B_position=_C(exB.GetPosition);\n",
    "\t\t\tif (B_position.length===0 && pending_pos.length>0 && limit_orders.length===0 && A_orders.length===0 && B_orders.length===0){\n",
    "\t\t\t\tLog('检测到Okex的仓位已被强平，执行自动释放对冲仓位。')\n",
    "\n",
    "\t\t\t\thedging_complete++\n",
    "\n",
    "\t\t\t\tfor (var thidx=0; thidx<pending_pos.length; ++thidx){\n",
    "\t\t\t\t\tvar posinfo=pending_pos[thidx]\n",
    "\t\t\t\t\tvar ticker=_C(posinfo.exchange.GetTicker)\n",
    "\t\t\t\t\tvar last=ticker.Last\n",
    "\t\t\t\t\tvar depth=_C(posinfo.exchange.GetDepth)\n",
    "\t\t\t\t\tvar buy1=depth.Bids[0].Price\n",
    "\t\t\t\t\tvar sell1=depth.Asks[0].Price\n",
    "\t\t\t\t\tvar account=_C(posinfo.exchange.GetAccount)\n",
    "\n",
    "\t\t\t\t\tif (posinfo.type==='spot-buy'){\n",
    "\t\t\t\t\t\tprocessor.force_limited_order('sell',posinfo.exchange,buy1,Math.min(account.Stocks,posinfo.amount),'spot')\n",
    "\t\t\t\t\t};\n",
    "\t\t\t\t\tif (posinfo.type==='spot-sell'){\n",
    "\t\t\t\t\t\tprocessor.force_limited_order('buy',posinfo.exchange,sell1,Math.min(account.Balance/last,posinfo.amount),'spot')\n",
    "\t\t\t\t\t};\n",
    "\t\t\t\t};\n",
    "\n",
    "\t\t\t\tpending_pos=[];\n",
    "\t\t\t};\n",
    "\n",
    "\n",
    "\t\t\t//status;\n",
    "\t\t\tvar table1 = {type: 'table', title: '交易所', cols: ['交易所','交易对','最新价格','钱','币','未完成订单','买一','卖一'], rows: []};\n",
    "\t\t\tvar table2 = {type: 'table', title: '统计', cols: ['轮询时间','当前价差','目标价差','对冲机会/实际对冲次数/对冲完成次数','USDT/USD','忽略波动范围'], rows: []};\n",
    "\t\t\tvar table3 = {type: 'table', title: '未完成对冲仓位', cols: ['交易所','类型','价格','数量','开仓时间'], rows: []};\n",
    "\t\t\tvar table4 = {type: 'table', title: '未完成定价单', cols: ['交易所','订单ID','发单日期'], rows: []};\n",
    "\t\t\tvar table5 = {type: 'table', title: '交易历史', cols: ['交易所','日期','类型', '成交数量','发单数量','成交价','发单价','备注'], rows: []};\n",
    "\t\t\tvar table6 = {type: 'table', title: '期货仓位', cols: ['交易所','持仓量','冻结量','持仓均价','实现盈余','类型','合约代码'], rows: []};\n",
    "\t\t\ttable1.rows.push([A_exname,A_currency,A_last+'(修正价:'+A_last_fixed+')',A_account.Balance,A_account.Stocks,A_orders.length,A_depth.Bids[0].Price,A_depth.Asks[0].Price]);\n",
    "\t\t\ttable1.rows.push([B_exname,B_currency,B_last,B_account.Balance,B_account.Stocks,B_orders.length,B_depth.Bids[0].Price,B_depth.Asks[0].Price]);\n",
    "\t\t\ttable2.rows.push([passedtime+'毫秒',cdiff2,target_bofu,hedging_op+'/'+hedging_real+'/'+hedging_complete,USDT_r,ignore_range]);\n",
    "\t\t\tfor (var thidx=0; thidx<pending_pos.length; ++thidx){\n",
    "\t\t\t\tvar posinfo=pending_pos[thidx]\n",
    "\t\t\t\ttable3.rows.push([posinfo.exchange.GetName(),posinfo.type,posinfo.price,posinfo.amount,posinfo.starttime])\n",
    "\t\t\t};\n",
    "\t\t\tfor (var idx2=0; idx2<limit_orders.length; ++idx2){\n",
    "\t\t\t\tvar info=limit_orders[idx2]\n",
    "\t\t\t\ttable4.rows.push([info.exchange.GetName(),info.ID,info.utcdate])\n",
    "\t\t\t};\n",
    "\t\t\tfor (var i=0; i < trades.length; i++){\n",
    "\t\t\t\ttable5.rows.push([trades[i].exname,trades[i].time,trades[i].type,trades[i].RealAmount,trades[i].WantAmount,trades[i].RealPrice,trades[i].WantPrice,trades[i].Memo])\n",
    "\t\t\t};\n",
    "\t\t\tfor (var i=0; i < B_position.length; i++){\n",
    "\t\t\t\ttable6.rows.push([B_exname,B_position[i].Amount,B_position[i].FrozenAmount,B_position[i].Price,B_position[i].Profit,B_position[i].Type,B_position[i].ContractType])\n",
    "\t\t\t};\n",
    "\n",
    "\t\t\tprocessor.logstatus=('`' + JSON.stringify([table1,table2,table3,table4,table6,table5])+'`'+'\\n');\n",
    "\t\t\tprocessor.stocksbalance=A_account.Stocks+B_account.Stocks;\n",
    "\n",
    "\t\t\t//sleep;\n",
    "\t\t\tSleep(wait_ms);\n",
    "\t\t};\n",
    "\n",
    "\t\treturn processor;\n",
    "\t};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    "\n",
    "};\n",
    ";\n",
    "\n",
    "};\n",
    "\n",
    "\n",
    "}\n",
    ";\n",
    "\n",
    "};\n",
    ";\n",
    "\n",
    "};\n",
    ";\n",
    "\n",
    "};\n",
    ";\n",
    "\n",
    "};\n",
    "\n",
    "\n",
    "}\n",
    ";\n",
    "\n",
    "};\n",
    "\n",
    "\n",
    "};\n",
    ";\n",
    "\n",
    "};\n",
    "\n",
    "\n",
    "};\n",
    "\n",
    "\n",
    "};\n",
    ";\n",
    "\n",
    "};\n",
    "\n",
    "\n",
    "};\n",
    "\n",
    "\n",
    "};\n",
    ";\n",
    "\n",
    "};\n",
    "\n",
    "\n",
    "};\n",
    "\n",
    "\n",
    "};\n",
    "\n",
    "\n",
    "};\n",
    ";\n",
    "\n",
    "};\n",
    "\n",
    "\n",
    "};\n",
    ";\n",
    "\n",
    "};\n",
    "\n",
    "\n",
    "};\n",
    ";\n",
    "\n",
    "};\n",
    "\n",
    "\n",
    "};\n",
    "\n",
    "\n",
    "};\n",
    "\n",
    "\n",
    "};\n",
    "\n",
    "\n",
    "}\n",
    "\n",
    "\n",
    "};\n",
    "\n",
    "\n",
    "}\n",
    "\n",
    "\n",
    "};\n",
    "\n",
    "\n",
    "}\""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
